#include "BatchCheckOutResponse.h"

BatchCheckOutResponse::BatchCheckOutResponse() {}

BatchCheckOutResponse::BatchCheckOutResponse(QString factoryNo, QString transId, QDateTime eventTime, QString resultCode, QList<BatchCheckOutResponsePcuInfo> pcuInfo):
    factoryNo(factoryNo),
    transId(transId),
    eventTime(eventTime),
    resultCode(resultCode),
    pcuInfo(pcuInfo)
{

}

bool BatchCheckOutResponse::fromJson(const QByteArray &jsonData)
{
    bool rtn = false;
    QJsonDocument doc = QJsonDocument::fromJson(jsonData);
    qDebug()<<doc.toVariant().toString();

    if(doc.isNull())
        rtn = false;
    else
    {
        QJsonObject json = doc.object();
        resultCode = json["resultCode"].toString();
        success = json["resultCode"].toBool();
        code = json["resultCode"].toInt();

        if(json["resultCode"].toInt() == 0)
        {
            factoryNo = json["factoryNo"].toString();
            transId = json["transId"].toString();
            eventTime = QDateTime::fromString(json["eventTime"].toString());
            const auto& pcuInfoArray = json["pcuInfo"].toArray();
            for(const auto& item:pcuInfoArray)
            {
                pcuInfo.append({
                                item.toObject().value("locationNo").toString(),
                                item.toObject().value("pcu").toString(),
                                item.toObject().value("resultMsg").toString()
                });
                resultMsg = item.toObject().value(QString("resultMsg")).toString();
            }
            rtn = true;

        }
        else
            rtn = false;
    }

    return rtn;
}


